package com.example.gulimallmember.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimallmember.dao.MemberLevelDao;
import com.example.gulimallmember.entity.MemberLevelEntity;
import com.example.gulimallmember.service.MemberLevelService;


@Service("memberLevelService")
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelDao, MemberLevelEntity> implements MemberLevelService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberLevelEntity> page = this.page(
                new Query<MemberLevelEntity>().getPage(params),
                new QueryWrapper<MemberLevelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R selectList(Map<String, Object> params) {

        // 安全地获取 key 参数
        Object keyObj = params.get("key");
        String keyValue = keyObj != null ? keyObj.toString() : null;

        LambdaQueryWrapper<MemberLevelEntity> queryWrapper = new LambdaQueryWrapper<>();

        // 只有当 keyValue 不为 null 且不为空字符串时才添加查询条件
        if (keyValue != null && !keyValue.isEmpty()) {
            queryWrapper.likeRight(MemberLevelEntity::getName, keyValue);
        }

        IPage<MemberLevelEntity> iPage = this.page(
                new Query<MemberLevelEntity>().getPage(params),
                queryWrapper
        );
        return R.ok().put("page", new PageUtils(iPage));
    }

}